package cn.autowired.exam.service.impl;

import cn.autowired.exam.dto.PaperDto;
import cn.autowired.exam.dto.StudentDto;
import cn.autowired.exam.service.ExcelDealService;
import cn.autowired.exam.utils.ExcelReadDealUtils;
import com.google.common.collect.Lists;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @version:
 */
@Service
public class ExcelDealServiceImpl implements ExcelDealService {


    @Override
    public  List<StudentDto> getExcelData(String excelFilePath) {
        // 获取表头转换成Map形式存储
        ExcelReadDealUtils.loadExcelFile(excelFilePath);
        Workbook workbook = ExcelReadDealUtils.workbook;
        Sheet sheet = workbook.getSheet("Sheet1");
        Map<Object, Object> headMap =ExcelReadDealUtils.getRowDataToMap(sheet.getRow(0), true);
        int lastRowNum = sheet.getLastRowNum();
        // 从索引为2开始，需注意根据实际情况来定
        ArrayList<StudentDto> students = Lists.newArrayList();
        NumberFormat nf = NumberFormat.getInstance();

        for (int i = 1; i <= lastRowNum; i++) {
            // 获取行对象
            StudentDto studentDto = new StudentDto();
            Row row = sheet.getRow(i);
            // 验证是否存在行
            if (ExcelReadDealUtils.isExistRow(row)) {
                // 获取一行指定列的信息
                List<Object> byGivenAttributeAndRowValue = ExcelReadDealUtils.getByGivenAttributeAndRowValue(headMap, row, new String[]{"id","学号","姓名","年级","班级","专业","性别"});

                for (int j = 0; j < byGivenAttributeAndRowValue.size(); j++) {

                    Object o = byGivenAttributeAndRowValue.get(j);
                    String value;
                    if (! (o instanceof String)){
                         value =nf.format(o);
                        if (value.indexOf(",") >= 0) {
                            value = value.replace(",", "");
                        }
                    } else {
                         value = (String) byGivenAttributeAndRowValue.get(j);
                    }

                    // id
                    if (j == 0) {
                        studentDto.setId(Integer.getInteger(value));
                    }
                    // studentNo
                    if (j == 1) {
                        studentDto.setStudentNo(value);
                    }
                    if (j == 2) {
                        studentDto.setName(value);
                    }
                    if (j == 3) {
                        studentDto.setGradeInfo(value);
                    }
                    if (j == 4) {
                        studentDto.setClassInfo(value);
                    }
                    if (j == 5) {
                        studentDto.setMajor(value);
                    }
                    if (j ==6) {

                        studentDto.setSex(value.equals("男")?1:0);
                    }

                }
                students.add(studentDto);
            }

        }
        return students;
    }

    @Override
    public List<PaperDto> getExcelDataForPaper(String excelFilePath) {
        return null;
    }
}

